<template>
  <!-- 外层容器 -->
  <view class="page-container">
    <!-- 可滚动区域 -->
    <scroll-view class="scroll-area" scroll-y>
      <view class="role-container">
        <!-- Logo区域 -->
        <view class="logo-section">
          <image class="logo" src="/static/home/logo.jpg" mode="aspectFit"></image>
          <text class="platform-name">GVR数字化加油维修解决方案平台</text>
        </view>

        <!-- 选择提示 -->
        <view class="select-hint">
          请选择注册角色
        </view>

        <view class="type-selection">
          <type-select-item
            text="加油站成员"
            :isActive="selectedType === USER_TYPES.GAS"
            @click="handleSelect(USER_TYPES.GAS)"
          />
          
          <type-select-item
            text="维修服务商成员"
            :isActive="selectedType == USER_TYPES.MAINTENANCE"
            @click="handleSelect(USER_TYPES.MAINTENANCE)"
          />
          
          <type-select-item
            text="设备供应商"
            :isActive="selectedType == USER_TYPES.SUPPLIER"
            @click="handleSelect(USER_TYPES.SUPPLIER)"
          />
          
          <type-select-item
            text="行业用户"
            :isActive="selectedType == USER_TYPES.CLIENT"
            @click="handleSelect(USER_TYPES.CLIENT)"
          />
        </view>
      </view>
    </scroll-view>

    <!-- 底部按钮 -->
    <view class="button-section">
      <button class="gvr-yellow-button surebutton" hover-class="none" @click="sureClick">确认选择</button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { USER_TYPES, userApi } from '@/service/api.js'
import { userStore } from '@/utils/userStore.js'
import { RouterMethods } from '@/utils/router'
const selectedType = ref('')

const handleSelect = (type) => {
   selectedType.value = type	
}
const sureClick = async() => {
	if (selectedType.value == '') {
		uni.showToast({
			title:'请选择身份',
			icon:'none'
		})
		return
	} else {
		try {
			const result = await userApi.updateUserType(selectedType.value)
			 if (result.code == undefined) {
				 uni.showToast({
				   title: '选择身份成功', 
				   icon: 'success' ,
				   duration:1000
				 })
				userStore.saveUserInfo(result)
				setTimeout(() => {
					RouterMethods.toHome()
				}, 1300)
			
			} else {
			   throw new Error(result.msg || '选择身份失败')
			}
		} catch (err) {
		  uni.showToast({
		    title: err.message || '选择身份失败',
		    icon: 'none'
		  })
		}
	}
}
</script>

<style scoped>
/* 外层容器样式 */
.page-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: $white-color;
}

.scroll-area {
  flex: 1;
  overflow: hidden;
  padding-bottom: calc(82rpx + env(safe-area-inset-bottom)); /* 添加底部内边距，预留按钮高度空间 */
}

/* role-container样式修改 */
.role-container {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 其他样式保持不变 */
.logo-section {
  margin-top: 162rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo {
  width: 476rpx;
  height: 108rpx;
  object-fit: contain;
}

.platform-name {
  margin-top: 26rpx;
 font-family: PingFangSC-Medium;
 font-size: 32rpx;
 color: #000000;
 letter-spacing: 0;
 font-weight: 500;
}

.select-hint {
  margin-top: 40rpx;
 font-family: PingFangSC-Medium;
 font-size: 32rpx;
 color: #000000;
 letter-spacing: 0;
 text-align: center;
 font-weight: 500;
}

.type-selection {
  /* background-color: red; */
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20rpx;
  padding-top: 40rpx;
  padding-bottom: 60rpx; /* 添加底部间距 */
}

/* 底部按钮区域样式修改 */
.button-section {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding-bottom: env(safe-area-inset-bottom);
  background-color: #FFFFFF;
  z-index: 999;
}

.surebutton {
  border-radius: 0;
}
</style>